package com.example.x6.configurationmaintenance.BoardCaseReceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;
import com.example.x6.configurationmaintenance.DataBase.ServiceDaemon.ServiceDaemonDatabaseOperation;
import com.example.x6.configurationmaintenance.DataBase.TimeTick.DatabaseOpration;
import com.example.x6.configurationmaintenance.Module.LogOperationManager;
import com.example.x6.configurationmaintenance.Service.RebootService;
import com.example.x6.configurationmaintenance.Utils.RebootUtil;

public class BoardCaseReceiverUtil extends BroadcastReceiver {
    private final int SERVICE_DAEMON_ADD = 0;
    private final int SERVICE_DAEMON_IGNOR = -1;
    private final int SERVICE_DAEMON_REMOVE = 1;
    private final String TAG = getClass().getSimpleName();
    private final int TYPE_POWER_KIT = 4;
    private final int TYPE_REBOOT_NOW = 1;
    private final String bootStart = "android.intent.action.BOOT_COMPLETED";
    private DatabaseOpration databaseOpration;
    private final String effectiveKey = "effective";
    private Handler handler = new Handler();
    private LogOperationManager logOperationManager;
    private final String powerTimeKey = "power_time";
    private final String powerTypeKey = "power_type";
    private final String serviceDaemon = "android.intent.action.MC_SERVICE_DAEMON";
    private ServiceDaemonDatabaseOperation serviceDaemonDatabaseOperation;
    private final String serviceDaemonEffectiveKey = "serviceDaemonEffectiveKey";
    private final String serviceDaemonNameKey = "serviceDaemonKey";
    private final String shutDown = "android.intent.action.MC_POWER_KIT";
    private final String shutDownDY = "android.intent.action.auto_power_shut";
    private final String shutDownNow = "android.intent.action.MCREBOOT";
    private final String shutDownTimeKey = "shut_time";

    public void onReceive(final Context context, Intent intent) {
        DatabaseOpration.initializeInstance(context);
        this.databaseOpration = DatabaseOpration.getInstance();
        ServiceDaemonDatabaseOperation.initializeInstance(context);
        this.serviceDaemonDatabaseOperation = ServiceDaemonDatabaseOperation.getInstance();
        LogOperationManager.initializeInstance(context);
        this.logOperationManager = LogOperationManager.getInstance();
        if (intent.getAction().equals("android.intent.action.MCREBOOT")) {
            Log.w(this.TAG, "立即重启");
            RebootUtil.reboot(context);
        } else if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            Intent intent1 = new Intent(context, RebootService.class);
            intent1.setAction("com.example.x6.configurationmaintenance.Service.RebootService");
            context.startService(intent1);
            this.logOperationManager.cleanLogProcessPID();
        } else {
            if (intent.getAction().equals("android.intent.action.MC_POWER_KIT") || intent.getAction().equals("android.intent.action.auto_power_shut")) {
                int type = intent.getIntExtra("power_type", 3);
                if (type == 1) {
                    Log.w(this.TAG, "立即重启");
                    RebootUtil.reboot(context);
                    return;
                } else if (type == 4) {
                    boolean isAdd = intent.getBooleanExtra("effective", true);
                    String[] taskTime = intent.getStringExtra("shut_time").split(":");
                    if (taskTime.length != 3) {
                        Toast.makeText(context, "传入时间格式错误", 0).show();
                    }
                    try {
                        int hour = Integer.parseInt(taskTime[0]);
                        int min = Integer.parseInt(taskTime[1]);
                        int sec = Integer.parseInt(taskTime[2]);
                        if (isAdd) {
                            String str = this.TAG;
                            Log.w(str, "添加定时任务:" + String.valueOf(hour) + ":" + String.valueOf(min) + ":" + String.valueOf(sec));
                            this.databaseOpration.insert(hour, min, sec, false, String.valueOf(System.currentTimeMillis()));
                        } else {
                            String str2 = this.TAG;
                            Log.w(str2, "取消定时任务:" + String.valueOf(hour) + ":" + String.valueOf(min) + ":" + String.valueOf(sec));
                            this.databaseOpration.delete(hour, min, sec);
                        }
                    } catch (Exception e) {
                        Toast.makeText(context, "传入时间格式错误", 0).show();
                        return;
                    }
                }
            }
            if (intent.getAction().equalsIgnoreCase("android.intent.action.MC_SERVICE_DAEMON")) {
                final String serviceName = intent.getStringExtra("serviceDaemonKey");
                switch (intent.getIntExtra("serviceDaemonEffectiveKey", -1)) {
                    case -1:
                        toast(context, "操作无效");
                        return;
                    case 0:
                        new Thread(new Runnable() {
                            /* class com.example.x6.configurationmaintenance.BoardCaseReceiver.BoardCaseReceiverUtil.AnonymousClass1 */

                            public void run() {
                                BoardCaseReceiverUtil.this.showServiceDaemonResult(context, BoardCaseReceiverUtil.this.serviceDaemonDatabaseOperation.insert(serviceName));
                            }
                        }).start();
                        return;
                    case 1:
                        new Thread(new Runnable() {
                            /* class com.example.x6.configurationmaintenance.BoardCaseReceiver.BoardCaseReceiverUtil.AnonymousClass2 */

                            public void run() {
                                int i = BoardCaseReceiverUtil.this.serviceDaemonDatabaseOperation.delete(serviceName);
                                Log.e(BoardCaseReceiverUtil.this.TAG, String.valueOf(i));
                                BoardCaseReceiverUtil.this.showServiceDaemonResult(context, i);
                            }
                        }).start();
                        return;
                    default:
                        toast(context, "操作无效");
                        return;
                }
            }
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void showServiceDaemonResult(Context context, int i) {
        if (i == 0) {
            toast(context, "操作成功");
        } else if (i == -2) {
            toast(context, "服务已存在");
        } else if (i == -4) {
            toast(context, "已到达守护服务最大数目，请先到关闭部分守护服务 \"配置维护->服务守护\"");
        } else if (i == -5 || i == -1) {
            toast(context, "系统中没有此服务");
        } else if (i == -3) {
            toast(context, "删除失败，没有找到此服务");
        }
    }

    private void toast(final Context context, final String string) {
        if (string != null) {
            this.handler.post(new Runnable() {
                /* class com.example.x6.configurationmaintenance.BoardCaseReceiver.BoardCaseReceiverUtil.AnonymousClass3 */

                public void run() {
                    Toast.makeText(context, string, 0).show();
                }
            });
        }
    }
}
